package com.imessage.action.no.errorphone;

import com.bean.BusinessErrorPhone;
import com.bean.BusinessUsePhone;
import com.imessage.comm.Constants;
import com.imessage.vo.UsePhoneVo;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/errorphone")
public class ErrorphoneAction extends ErrorphoneBaseAction {

    @RequestMapping(value = "")
    public Object run(UsePhoneVo usephoneVo) {
        List<Expression> expressionList = new ArrayList<Expression>();
        setExpressionList(expressionList, usephoneVo);
        Page<BusinessErrorPhone> page = baseService.findPage(BusinessErrorPhone.class, expressionList,
                null, getOrder(usephoneVo.getOrders()), usephoneVo.getPageCurrent(),
                usephoneVo.getPageSize());
        return page;
    }

    @RequestMapping(value = "initpage")
    public ModelAndView initpage(
            @RequestParam(name = "req_id", required = true) String id) {
        ModelAndView modelAndView = new ModelAndView();
        BusinessErrorPhone errorPhone = new BusinessErrorPhone();
        if (StringUtils.isNotBlank(id)) {
            errorPhone = baseService.findById(BusinessErrorPhone.class, id);
            modelAndView.setViewName("admin/no/errorphone/modify");
        } else {
            modelAndView.setViewName("admin/no/errorphone/add");
        }
        setModelAndView(modelAndView, errorPhone);
        return modelAndView;
    }


    @RequestMapping(value = "add")
    public Object add(UsePhoneVo usephoneVo) {
        BusinessErrorPhone errorPhone = new BusinessErrorPhone();
        setErrorPhone(usephoneVo, errorPhone);
        int state = baseService.saveObject(errorPhone);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }


    @RequestMapping(value = "modify")
    public Object modify(UsePhoneVo usephoneVo) {
        BusinessErrorPhone errorPhone = baseService.findById(BusinessErrorPhone.class, usephoneVo.getId());
        setErrorPhone(usephoneVo, errorPhone);
        int state = baseService.updateObject(errorPhone);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return errorPhone;
        }
        return getJsonForErr("", datagrid);
    }

    @RequestMapping(value = "del")
    public Object del(@RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            BusinessErrorPhone errorPhone = baseService.findById(BusinessErrorPhone.class, id);
            state = baseService.delObject(errorPhone);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }


}
